/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package org.dromara.setting.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.setting.domain.bo.SettingConfigItemBo;
import org.dromara.setting.domain.vo.*;
import org.dromara.setting.service.ISettingConfigItemService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 渠道配置-后台管理
 *
 * @author LXK-RY5
 * @date 2024-08-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/setting/configItem")
public class SettingConfigItemController extends BaseController {

    private final ISettingConfigItemService settingConfigItemService;

    /**
     * 查询配置项列表
     */
    @SaCheckPermission("setting:configItem:list")
    @GetMapping("/list")
    public TableDataInfo<SettingConfigItemVo> list(SettingConfigItemBo bo, PageQuery pageQuery) {
        return settingConfigItemService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询公众号配置
     */
    @SaCheckPermission("setting:configItem:wxmp")
    @GetMapping("/wxmp/detail")
    public R<ChannelOaVo> detail() {
        return R.ok(settingConfigItemService.wxMpDetail());
    }

    /**
     * 查询小程序配置
     */
    @SaCheckPermission("setting:configItem:wxmini")
    @GetMapping("/wxmini/detail")
    public R<ChannelMpVo> wxMiniDetail() {
        return R.ok(settingConfigItemService.wxMiniDetail());
    }

    /**
     * 导出配置项列表
     */
    @SaCheckPermission("setting:configItem:export")
    @Log(title = "配置项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SettingConfigItemBo bo, HttpServletResponse response) {
        List<SettingConfigItemVo> list = settingConfigItemService.queryList(bo);
        ExcelUtil.exportExcel(list, "配置项", SettingConfigItemVo.class, response);
    }

    /**
     * 获取配置项详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("setting:configItem:query")
    @GetMapping("/{id}")
    public R<SettingConfigItemVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(settingConfigItemService.queryById(id));
    }

    /**
     * 新增配置项
     */
    @SaCheckPermission("setting:configItem:add")
    @Log(title = "配置项", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SettingConfigItemBo bo) {
        return toAjax(settingConfigItemService.insertByBo(bo));
    }

    /**
     * 修改配置项
     */
    @SaCheckPermission("setting:configItem:edit")
    @Log(title = "配置项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SettingConfigItemBo bo) {
        return toAjax(settingConfigItemService.updateByBo(bo));
    }

    /**
     * 删除配置项
     *
     * @param ids 主键串
     */
    @SaCheckPermission("setting:configItem:remove")
    @Log(title = "配置项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(settingConfigItemService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量修改配置项
     */
    @SaCheckPermission("setting:configItem:bulkedit")
    @Log(title = "配置项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/bulk/edit")
    public R<Void> bulkEdit(@Validated(EditGroup.class) @RequestBody List<SettingConfigItemBo> bo) {
        return toAjax(settingConfigItemService.bulkEdit(bo));
    }

    /**
     * 查询余额支付配置
     */
    @SaCheckPermission("setting:configItem:yueconfig")
    @GetMapping("/yue/detail")
    public R<ChannelYueVo> yueDetail() {
        return R.ok(settingConfigItemService.yueDetail());
    }

    /**
     * 查询支付宝支付配置
     */
    @SaCheckPermission("setting:configItem:allconfig")
    @GetMapping("/all/detail")
    public R<ChannelAllVo> allDetail() {
        return R.ok(settingConfigItemService.allDetail());
    }

    /**
     * 查询微信支付配置
     */
    @SaCheckPermission("setting:configItem:allconfig")
    @GetMapping("/wxpay/detail")
    public R<ChannelWXPayVo> wxPayDetail() {
        return R.ok(settingConfigItemService.wxPayDetail());
    }

    /**
     * 协议配置项
     */
    @SaCheckPermission("setting:configItem:useragreement")
    @GetMapping("/user/agreement")
    public R<AgreementVo> userAgreement() {
        return R.ok(settingConfigItemService.userAgreement());
    }


    @SaCheckPermission("setting:configItem:isShou")
    @GetMapping("/pay/isshou/{id}")
    @Log(title = "修改支付状态", businessType = BusinessType.UPDATE)
    public R<Void> updatePayStatus(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return toAjax(settingConfigItemService.payIsShow(id));
    }

    /**
     * 查询连线客openApi配置
     */
    @SaCheckPermission("setting:configItem:lxkopenapi")
    @GetMapping("/lxkopenapi/detail")
    public R<LxkOpenApiVo> lxkOpenApiDetail() {
        return R.ok(settingConfigItemService.lxkOpenApiDetail());
    }

    /**
     * 查询连线客omsOrder配置
     */
    @SaCheckPermission("setting:configItem:omsorder")
    @GetMapping("/omsorder/detail")
    public R<LxkOmsOrderVo> omsOrderDetail() {
        return R.ok(settingConfigItemService.omsOrderDetail());
    }
}
